package com.powernobug.service.impl;

import com.github.pagehelper.PageHelper;
import com.powernobug.bean.model.MarketAd;
import com.powernobug.bean.model.MarketAdExample;
import com.powernobug.bean.model.MarketCoupon;
import com.powernobug.bean.model.MarketStorage;
import com.powernobug.bean.vo.BaseRespVo;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.param.AdQueryParam;
import com.powernobug.bean.vo.param.BaseQueryParam;
import com.powernobug.mapper.MarketAdMapper;
import com.powernobug.service.AdminAdService;
import com.powernobug.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @project: project1-no-bug
 * @package: com.powernobug.service
 * @author: HuangWeiLong
 * @date: 2024/8/23 9:16
 */
public class AdminAdServiceImpl implements AdminAdService {
    @Override
    public PageVo list(AdQueryParam adQueryParam) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketAdMapper mapper = sqlSession.getMapper(MarketAdMapper.class);
        //分页再查找
        PageHelper.startPage(adQueryParam.getPage(),adQueryParam.getLimit());

        MarketAdExample marketAdExample = new MarketAdExample();
        marketAdExample.setOrderByClause(adQueryParam.getSort()+" "+adQueryParam.getOrder());
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();
        if(adQueryParam!=null) {
            if (adQueryParam.getContent() != null && adQueryParam.getContent().length() > 0) {
                criteria.andContentLike("%" + adQueryParam.getContent() + "%");
            }
            if (adQueryParam.getName() != null && adQueryParam.getName().length() > 0) {
                criteria.andNameLike("%" + adQueryParam.getName() + "%");
            }
        }
        List<MarketAd> marketAds = mapper.selectByExample(marketAdExample);


        return PageVo.list(marketAds);
    }

    @Override
    public BaseRespVo create(MarketAd marketAd) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketAdMapper mapper = sqlSession.getMapper(MarketAdMapper.class);
        marketAd.setAddTime(new Date());
        marketAd.setUpdateTime(new Date());
        marketAd.setDeleted(false);
        mapper.insert(marketAd);
        return BaseRespVo.ok(marketAd);
    }

    @Override
    public MarketAd fetch(String url) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketAdMapper mapper = sqlSession.getMapper(MarketAdMapper.class);
        return mapper.selectByUrl(url);
    }

    @Override
    public BaseRespVo update(MarketAd marketAd) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketAdMapper mapper = sqlSession.getMapper(MarketAdMapper.class);

        mapper.updateByPrimaryKey(marketAd);
        return BaseRespVo.ok(marketAd);
    }

    @Override
    public void delete(MarketAd marketAd) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketAdMapper mapper = sqlSession.getMapper(MarketAdMapper.class);
        mapper.deleteByPrimaryKey(marketAd.getId());
    }
}
